package org.itzheng.itplayer.data

import android.Manifest
import android.view.View
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.itzheng.and.activity.permissions.IPermissionManager.OnRequestPermissionListener
import org.itzheng.itplayer.MainActivity
import org.itzheng.itplayer.R
import org.itzheng.itplayer.bean.MyMenuItem
import org.itzheng.itplayer.local.LocalVideoFragment
import org.itzheng.itplayer.player.VideoPlayActivity
import java.util.*

/**
 * Title:<br></br>
 * Description: <br></br>
 *
 * @email ItZheng@ZoHo.com
 * Created by itzheng on 2020-12-4.
 */
object DataMainMenu {
    fun getMainMenus(activity: MainActivity): List<MyMenuItem> {
        val mItems: MutableList<MyMenuItem> = ArrayList()
        var item: MyMenuItem
        item = MyMenuItem()
        item.name = "本地视频"
        item.iconRes = R.drawable.ic_main_media
        item.onClickListener = View.OnClickListener {
            activity.requestPermissions(object : OnRequestPermissionListener {
                override fun grantPermissions(isAllGrant: Boolean, permissions: Array<String>) {
                    activity.startFragment(LocalVideoFragment.newInstance())
                }

                override fun denyPermissions(isAllDeny: Boolean, permissions: Array<String>) {}
            }, Manifest.permission.WRITE_EXTERNAL_STORAGE)
        }
        mItems.add(item)
        item = MyMenuItem()
        item.name = "播放列表"
        item.iconRes = R.drawable.ic_main_list
        mItems.add(item)
        item = MyMenuItem()
        item.name = "我的最爱"
        item.iconRes = R.drawable.ic_main_favorite
        mItems.add(item)
        item = MyMenuItem()
        item.name = "播放历史"
        item.iconRes = R.drawable.ic_main_history
        item.onClickListener = View.OnClickListener {
            activity.requestPermissions(object : OnRequestPermissionListener {
                override fun grantPermissions(isAllGrant: Boolean, permissions: Array<String>) {
                    GlobalScope.launch(Dispatchers.IO) {
                        VideoPlayActivity.startActivity(
                            activity,
                            DataVideoPlay.getLastList(),
                            DataVideoPlay.getLastListPosition()
                        )
                    }
                }

                override fun denyPermissions(isAllDeny: Boolean, permissions: Array<String>) {}
            }, Manifest.permission.WRITE_EXTERNAL_STORAGE)
        }
        mItems.add(item)
        return mItems
    }
}